// import 'package:flutter/material.dart';
// import 'package:webview_flutter/webview_flutter.dart';
//
// class SmallVideoPage extends StatefulWidget {
//   const SmallVideoPage({super.key});
//
//   @override
//   State<SmallVideoPage> createState() => _SmallVideoPage();
// }
//
// class _SmallVideoPage extends State<SmallVideoPage> {
//   late final WebViewController _controller;
//
//   @override
//   void initState() {
//     super.initState();
//     _controller = WebViewController()
//       ..setJavaScriptMode(JavaScriptMode.unrestricted)
//       ..setBackgroundColor(const Color(0x00000000))
//       ..loadRequest(Uri.parse(
//         'https://v1.kwaicdn.com/ksc2/_saDp_o_AT6sqPXU1JtEYfwA1Qf_l-aDZrpEsqAjm8K0ETPVDq2gTbehiIPofULBfGqHWjM63QEYHnBuUmO7hLOxsLkNYohDJ3lJ8dAEYMQlNxGpHwS0hMK2R7J8NwU_b7XlX_CQ-J27r3LZOBtZPoBbCSFiCYLgvxCsEh0XeCGySXLuPXkAhklb6KtlMz2N.mp4?pkey=AAUgQrWdhSPk16Pn6gaR3wGHl1SoTq0mC0XS_rrJZi-og2_PmMoEEbrzyHT9cQncJ0B-KIOfXJMihF51Kqv7uiBcdVjJ7EzabaZoaya6KkXHPMlf79KEbvNa-lkbiegmD4k&tag=1-1732865813-unknown-0-zkghjh63n0-a75de8b94421ee67&clientCacheKey=3xk3fibbc5ikci9_b.mp4&di=2606a49b&bp=14944&tt=b&ss=vp',
//       ));
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       body: WebViewWidget(controller: _controller),
//     );
//   }
// }

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert'; // 用于解析 JSON
import 'package:webview_flutter/webview_flutter.dart';

class SmallVideoPage extends StatefulWidget {
  const SmallVideoPage({Key? key}) : super(key: key);

  @override
  State<SmallVideoPage> createState() => _SmallVideoPage();
}

class _SmallVideoPage extends State<SmallVideoPage> {
  late final WebViewController _controller;
  String? url; // 存储视频 URL

  @override
  void initState() {
    super.initState();
    _controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000));
    fetchVideoUrl(); // 获取视频数据
  }

  // 获取视频数据的函数
  Future<void> fetchVideoUrl() async {
    const String apiUrl = "http://api.yujn.cn/api/ks_sj.php?count=1";
    try {
      final response = await http.get(Uri.parse(apiUrl));
      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        final videoData = data['data'][0]; // 获取第一个视频的数据
        setState(() {
          url = videoData['video']; // 存储视频 URL
        });
        // print(url);
        // 加载新的视频
        if (url != null) {
          _controller.loadRequest(Uri.parse(url!));
        }
      } else {
        print("请求失败，状态码：${response.statusCode}");
      }
    } catch (e) {
      print("请求错误：$e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '上下滑动切换视频，由于视频加载较慢，滑动后请耐心等待',
          style: TextStyle(fontSize: 12),
        ),
      ),
      body: GestureDetector(
        onVerticalDragEnd: (details) {
          if (details.primaryVelocity! > 0) {
            // 向下滑动（加载上一视频逻辑可以添加）
            print("下滑");
            fetchVideoUrl(); // 加载新视频
          } else if (details.primaryVelocity! < 0) {
            // 向上滑动
            print("上滑");
            fetchVideoUrl(); // 加载新视频
          }
        },
        child: url == null
            ? const Center(child: CircularProgressIndicator()) // 数据加载时显示加载动画
            : WebViewWidget(controller: _controller), // 显示 WebView
      ),
    );
  }
}



